import AntModal from "@/components/pt/dialog/AntModal";
import FileUpload from "@/components/pt/fileupload/IndexList";
import LinshideptList from "@/views/biz/linshideptmanage/indexlist";
import {
  getBizLinshiManage,
  addBizLinshiManage,
  updateBizLinshiManage,
  findMaxSort,
  getUUID,
} from "@/api/biz/bizLinshiManage";
import {
  addBizAssessToDeptDetailsFile,
  listBizAssessToDeptDetailsFile,
} from "@/api/biz/bizAssessToDeptDetailsFile";
export default {
  name: "CreateForm",
  props: {
    statusOptions: {
      type: Array,
      required: true,
    },
    deptOptions: {
      type: Array,
      required: true,
    },
  },
  components: {
    AntModal,
    FileUpload,
    LinshideptList,
  },
  data() {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      total: 0,
      id: undefined,
      formTitle: "添加临时督办",
      // 表单参数
      form: {},
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        deptIds: [
          { required: true, message: "请选择监督企业", trigger: "change" },
        ],
      },
      fileList: [],
    };
  },
  filters: {},
  created() {},
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    uploadCompleteFile(item, items) {
      console.log(("item", item, items));
      var params = {
        assessDeptDetailsId: this.form.uuid,
        fileUrl: item.url,
        fileName: item.fileName,
      };
      addBizAssessToDeptDetailsFile(params).then((response) => {
        this.$message.success("上传成功", 3);
      });
      this.fileList = items;
    },
    handlerRemoveFile(file) {
      this.$message.success("删除成功", 3);
    },
    onClose() {
      this.open = false;
      this.reset();
      this.$emit("ok");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("ok");
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        title: undefined,

        fileName: undefined,

        fileUrl: undefined,

        content: undefined,

        status: undefined,
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      /** 获取最大编号 */
      findMaxSort().then((response) => {
        this.form.sort = response.data;
        this.formTitle = "添加临时督办";
      });
      getUUID().then((response) => {
        this.form.uuid = response.data;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row, isdetail) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const bizLinshiManageId = row.id;
      getBizLinshiManage(bizLinshiManageId).then((response) => {
        this.form = response.data;
        this.form.deptIds = JSON.parse(this.form.deptIds);
        if (isdetail) {
          this.formTitle = "临时督办详情";
        } else {
          this.formTitle = "修改临时督办";
        }
        listBizAssessToDeptDetailsFile({
          assessDeptDetailsId: this.form.id,
        }).then((response) => {
          this.fileList = response.data.list;
        });
        this.spinning = !this.spinning;
      });
    },
    /** 修改按钮操作 */
    handleDetails(row, isdetail) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const bizLinshiManageId = row.id;
      getBizLinshiManage(bizLinshiManageId).then((response) => {
        this.form = response.data;
        this.form.deptIds = JSON.parse(this.form.deptIds);
        this.formTitle = "临时督办详情";
        this.spinning = !this.spinning;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.deptIds = JSON.stringify(this.form.deptIds);
          this.form.deptNames = JSON.stringify(this.form.deptNames);
          const saveForm = JSON.parse(JSON.stringify(this.form));
          if (this.form.id !== undefined) {
            updateBizLinshiManage(saveForm).then((response) => {
              this.$message.success("修改成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          } else {
            addBizLinshiManage(saveForm).then((response) => {
              this.$message.success("新增成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          }
        } else {
          return false;
        }
      });
    },
    back() {
      const index = "/biz/linshimanage/index";
      this.$router.push(index);
    },
    onTreeChange(v, label) {
      console.log("onchange", v, label);
      this.form.deptIds = v;
      this.form.deptNames = label;
    },
    onSelect(v, node, extra) {
      console.log("onSelect:", v, node, extra);
    },
  },
};
